package com.ruoyi.mateproduction.controller;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.mateproduction.domain.MateReturn;
import com.ruoyi.mateproduction.service.MateReturnService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @Author
 * @Version 1.0
 */
@RestController
@RequestMapping("/mateReturn")
public class MateReturnController extends BaseController {

    @Autowired
    MateReturnService mateReturnService;

    /**
     * 保存退料单
     */
    @Log(title = "保存退料单", businessType = BusinessType.INSERT)
    @PostMapping("/insertMateReturn")
    public AjaxResult insertMateReturn(@RequestBody MateReturn mateReturn) {
        mateReturn.setDeptId(SecurityUtils.getLoginUser().getCompanyId());
        int num = mateReturnService.insertMateReturn(mateReturn);
        if (num > 0) {
            return AjaxResult.success("添加成功");
        } else {
            return AjaxResult.error("添加失败");
        }
    }

    /**
     * 查询退料单
     */
    @GetMapping("/MateReturnList")
    @ResponseBody
    public TableDataInfo MateReturnList(@RequestParam(value = "beginTime", required = false) String beginTime,
                                        @RequestParam(value = "endTime", required = false) String endTime) {
        MateReturn mateReturn = new MateReturn();
        mateReturn.setBeginTime(beginTime);
        mateReturn.setEndTime(endTime);
        startPage();
        mateReturn.setDeptId(SecurityUtils.getLoginUser().getUser().getCompanyId());
        List<MateReturn> list = mateReturnService.selectMateReturnList(mateReturn);
        return getDataTable(list);
    }

    /*
       查询已经做完的退料单
   */
    @GetMapping("/selectMateReturn")
    public MateReturn selectMateReturn(@RequestParam(value = "mateReturnId", required = false) Long mateReturnId) {
        MateReturn mateReturn = mateReturnService.selectMateReturn(mateReturnId);
        return mateReturn;
    }

    /*
     * 删除退料单
     */
    @Log(title = "删除退料单", businessType = BusinessType.DELETE)
    @GetMapping("/remove/{mateReturnId}")
    @ResponseBody
    public AjaxResult remove(@PathVariable("mateReturnId") Long mateReturnId) {
        mateReturnService.remove(mateReturnId);
        return AjaxResult.success("删除成功");
    }

    /*
        编辑退料单
           */
    @PostMapping("/updateMateReturn")
    @ResponseBody
    public AjaxResult updateMateReturn(@RequestBody MateReturn mateReturn) {
        int num = mateReturnService.updateMateReturn(mateReturn);
        if (num > 0) {
            return AjaxResult.success("修改成功");
        } else {
            return AjaxResult.error("修改失败");

        }
    }
}
